// pages/mine/mine.js
var wxUser = wx.getStorageSync('wxUser');     //授权信息
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    noaccredit: false,
    noaccreditPhone:false,
    user:'',      //用户信息
    orderMenu:[{
      imgUrl:'../../images/orderPay-icon@2x.png',
      text:'待付款'
    },{
      imgUrl:'../../images/orderSend-icon@2x.png',
      text:'待发货'
    },{
      imgUrl:'../../images/orderShou-icon@2x.png',
      text:'待收货'
    },{
      imgUrl:'../../images/orderAll-icon@2x.png',
      text:'所有订单'
    }],
    fixedTop:70,
    fixedLeft:70,
    isMyrecommend:false,      //是否显示我的团队
    show_Service_Star:false,   //是否显示服务之星
    star_array:[],
    Reward:0,
    Integral:0,
  },

  /**
   * 我的团队
   */
  showMyrecommend() {
    this.setData({
      isMyrecommend:true
    })
  },
  //显示隐藏服务之星
  Service_Star(){
    var that = this
    var Star = that.data.show_Service_Star;
    that.setData({
      show_Service_Star: !Star
    })
  },
  //服务之星->预约
  go_appoint(e){
    var data = e.target.dataset;
    console.log(data)
    wx.navigateTo({
      url: '../appointmentDay/appointmentDay?shopid=' + data.shop + '&employee=' + data.id,
    })
  },
  // 跳转到积分详情页面
  go_IntegralDetail(){
    wx.navigateTo({
      url: '../IntegralDetail/IntegralDetail',
    })
  },
  /**关闭我的团队 */
  closeSpec() {
    this.setData({
      isMyrecommend:false
    })
  },
  /**
   * 跳转到我的订单
   */
  toMyOrders(e) {
    wx.navigateTo({
      url: '../myOrders/myOrders?type=' + e.currentTarget.dataset.type,
    })
  },
  /**获取授权成功 */
  login() {
    console.log(wxUser);
    this.setData({
      noaccredit: false,
      noaccreditPhone:true,
      user:wxUser,
    })
  },
  /**获取手机号授权成功 */
  loginPhone(e) {
    this.setData({
      noaccreditPhone: false,
      user:e.detail
    })
    this.onLoad();    //刷新页面
  },
  fixedMove(e) {
    let moveX = e.changedTouches[0].pageX;
    let moveY = e.changedTouches[0].pageY;
    this.setData({
      fixedTop: moveY-40,
      fixedLeft: moveX-40
    })
  },
  /**获取用户信息 */
  customerData() {
    var that = this;
    var uid = wx.getStorageSync('user').uid;
    let user = that.user;
    app.request({
      url: app.getApi('Customers/customer_balance'),
      data: {
        customer_id: uid
      },
      success: function (res) {
        var data = res.detail;
        // data.BirthDate ? data.BirthDate = data.BirthDate.substr(0, 10) : data.BirthDate = '';
        // data.Province ? '' : data.Province = '';
        // data.City ? '' : data.City = '';
        // data.Area ? '' : data.Area = '';
        // data.BankName ? '' : data.BankName = '';
        // user.Name = data.Name
        that.setData({
          user: data
        })
      },
      fail: function (error) {
        app.showToast({
          title: error.msg,
          duration: 1500,
          success:function(){
            //清除缓存
            try {
              wx.removeStorageSync('user');
            } catch (e) {
              that.onLoad();
            }
          }
        })
      }
    })
  },

  /**
 * 绑定上下级
 */
  bingdRelation: function (shareUid) {
    console.log('shareUid',shareUid)
    var uid = wx.getStorageSync('user').uid;
    console.log('uid',uid)
    app.request({
      url: app.getApi('login/updateIntroducer'),
      data: { superiorid: shareUid, Id: uid },
      sync: true,
      success: function (data) {
        console.log('success',data)
      },
      fail: function (error) {
        console.log('err',error);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options);
    var wxUser = wx.getStorageSync('wxUser');     //授权信息
    var user = wx.getStorageSync('user');         //已注册用户信息
    var that=this;
    if (options && options.shareUid){
      console.log(options)
      wx.setStorage({
        key: 'shareUid',
        data: options.shareUid
      })
    }
    if (wxUser == '') {   //未授权登录
      this.setData({
        noaccredit: true
      })
    }else{
      console.log(options)
      if (options && options.shareUid) {
        that.bingdRelation(options.shareUid);
      }
      this.setData({
        user: wxUser.userInfo,
      })
      if(user == '') {
        this.setData({
          noaccreditPhone: true
        })
      } else {
        this.setData({
          // user: user,
          noaccreditPhone: false
        })
      }
    }
    this.customerData();    //获取用户信息
    this.customer_data();//获取个人的花瓣和积分
    this.Petal_integral();//各种订单数量
    this.server_star();//服务之星的数据获取
  },
  //获取个人的花瓣和积分
  customer_data:function(){
    var that = this;
    var user = wx.getStorageSync('user');         //已注册用户信息
    app.request({
      url: app.getApi('Customers/customer_balance'),
      data: {
        customer_id: user.uid
      },
      success: function (res) {
        if (res.detail){
          that.setData({
            Reward: res.detail.Integral,
            Integral: res.detail.Reward
          })
        }
      },
      fail: function (error) {
        app.showToast({
          title: error.msg,
          duration: 1500
        })
      }
    })
  },
  //各种订单数量
  Petal_integral:function(){
    var that = this
    var user = wx.getStorageSync('user');         //已注册用户信息
    var orderMenu = that.data.orderMenu;
    app.request({
      url: app.getApi('Customers/ordernum_list'),
      data:{
        customer_id:user.uid
      },
      success: function (res) {
        orderMenu[0].num = res.nopayNum;
        orderMenu[1].num = res.shipnum;
        orderMenu[2].num = res.receiptnum;
        orderMenu[3].num = res.allNum;
        that.setData({
          orderMenu: orderMenu
        })
      },
      fail: function (error) {
        app.showToast({
          title: error.msg,
          duration: 1500
        })
      }
    })
  },
  //服务之星的数据获取
  server_star(){
    var that=this;
    var user = wx.getStorageSync('user');         //已注册用户信息
    app.request({
      url: app.getApi('Appointment/point_score_list'),
      data: {
        user_id: user.uid
      },
      success: function (res) {
        var list = res.list
        list.forEach(value => {
          if (value.headImg){
            value.headImg = app.getImgUrl(value.headImg);
          }
        })
        that.setData({
          star_array: list
        })
      },
      fail: function (error) {
        // app.showToast({
        //   title: error.msg,
        //   duration: 1500
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    wx.getSystemInfo({
      success(res) {
        that.setData({
          fixedTop: res.windowHeight-100,
          fixedLeft: res.windowWidth-100
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();    //刷新页面
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return (app.share());
  }
})